package com.example.demo.service.impl;

import com.example.demo.DTO.CartGoodsSellerDTO;
import com.example.demo.DTO.CartGoodsSellerInfo;
import com.example.demo.DTO.CartItemDTO;
import com.example.demo.DTO.StoreDTO;
import com.example.demo.mapper.CartMapper;
import com.example.demo.mapper.PetsMapper;
import com.example.demo.mapper.SellerMapper;
import com.example.demo.pojo.Cart;
import com.example.demo.pojo.Pets;
import com.example.demo.pojo.Seller;
import com.example.demo.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CartServiceImpl implements ICartService {

	@Autowired
	private CartMapper cartMapper;

	@Autowired
	private PetsMapper goodsMapper;

	@Autowired
	private SellerMapper sellerMapper;
    @Override
	public Cart addToCart(Cart cart) {
		cart.setCreateTime(LocalDate.now());
		cart.setTotalPrice(cart.getPrice()*cart.getNum());
		cartMapper.insert(cart);
		return cart;
	}

	@Override
	public List<Cart> list(Integer userID) {
		return cartMapper.list(userID);
	}

	@Override
	public List<Cart> getSelectedCartItemsByUser(Integer userId) {
		List<Cart> carts = cartMapper.getSelectedCartItemsByUser(userId);
		for (Cart cart : carts) {
			Seller sellerInfo = sellerMapper.selectByID(cart.getSellerID());
			Pets productInfo = goodsMapper.findByGoodsID(cart.getGoodsID());
			cart.setGoodsName(productInfo.getGoodsName());
			cart.setSellerName(sellerInfo.getSellerName());
		}
		return carts;
	}

	@Override
	public CartGoodsSellerInfo findGoodsAndSellersByUserID(Integer userID) {
		List<CartGoodsSellerDTO> cartGoodsSellerDTOs = cartMapper.findGoodsAndSellerIDsByUserID(userID);
		if (cartGoodsSellerDTOs.isEmpty()) {
			return new CartGoodsSellerInfo(new ArrayList<>(), new ArrayList<>());
		}

		List<Integer> goodsIDs = cartGoodsSellerDTOs.stream()
				.map(CartGoodsSellerDTO::getGoodsID)
				.distinct()
				.collect(Collectors.toList());

		List<Integer> sellerIDs = cartGoodsSellerDTOs.stream()
				.map(CartGoodsSellerDTO::getSellerID)
				.distinct()
				.collect(Collectors.toList());

		List<Pets> goodsList = goodsMapper.findGoodsByIDs(goodsIDs);
		List<Seller> sellerList = sellerMapper.findSellersByIDs(sellerIDs);

		// 将 num 添加到 Goods 中
		Map<Integer, Integer> goodsNumMap = cartGoodsSellerDTOs.stream()
				.collect(Collectors.toMap(CartGoodsSellerDTO::getGoodsID, CartGoodsSellerDTO::getNum));

		goodsList.forEach(pets -> pets.setNum(goodsNumMap.get(pets.getGoodsID())));


		// 查询数据库获取 selected 属性
		Map<Integer, Boolean> goodsSelectedMap = cartGoodsSellerDTOs.stream()
				.collect(Collectors.toMap(CartGoodsSellerDTO::getGoodsID, CartGoodsSellerDTO::isSelected));

         // 更新goodsList中的商品选中状态
		goodsList.forEach(pets -> {
			if (goodsSelectedMap.containsKey(pets.getGoodsID())) {
				pets.setSelected(goodsSelectedMap.get(pets.getGoodsID()));
			}
		});
		return new CartGoodsSellerInfo(goodsList, sellerList);
	}

	@Transactional
	@Override
	public void updateStoreSelectionStatus(Integer userID, Integer sellerID, Boolean selected) {
		// 更新该商家下的所有商品选中状态
		List<Cart> cartItems = cartMapper.findByUserAndSellerID(userID, sellerID);
		for (Cart item : cartItems) {
			cartMapper.updateCartItemSelectionStatus(item.getGoodsID(), userID, selected);
		}
	}

	@Transactional
	@Override
	public void updateCartItemSelectionStatus(Integer userID, Integer goodsID, Boolean selected) {
		cartMapper.updateCartItemSelectionStatus(goodsID, userID, selected);
	}

	public void removeCartItem(Integer CartID) {
		cartMapper.deleteById(CartID);
	}
}
